package com.service.atm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.IService;

/**
 * 1000 返回账号校验报文
 * 
 * @author menlin
 * 
 */
public class Answer_atm_account_check implements IService {
	private JdbcWorker jw;// = new JdbcWorker();

	@Override
	public VO exec(VO arg0) {
		try {
			// long INSTANCEID = getBizid();
			// String bizid = new SimpleDateFormat("yyMMddHHmmss").format(new
			// Date()) + String.format("%08d", INSTANCEID);
			jw = new JdbcWorker();
			return this.packResult(arg0);
		} catch (Exception e) {
			CommonUtil.debug("Answer_atm_account_check  error", e);
		}
		return null;
	}

	/**
	 * 创建报文结果
	 * 
	 * @param bizid
	 *            String
	 * @param arg
	 *            VO
	 * @return VO
	 * @throws Exception
	 */
	private VO packResult(VO arg) throws Exception {
		VO vo = new VO();
		StringBuffer message = new StringBuffer(100);
		message.append("00000000"); // 流水号
		message.append("00000000"); // 交易报文总长度 U_char[8]
		message.append("1000"); // 交易码 u_char[4]
		message.append("0000"); // 应答结果 u_char[4]
		Vector<VO> vos = checkAccount(arg);
		boolean result = (vos != null);
		message.append(result ? "00000004R0010000" : "00000004R0019999");
		String des = result ? "success" : "fail";
		message.append(String.format("%08d", des.getBytes("gb2312").length)
				+ "R002" + des);
		// 成功时返回账号集合
		if (result) {
			int len = vos.size(); // 账号数量 R003
			message.append(String.format("%08d", (len + "").length()) + "R003"
					+ len);
			int i = 0;
			for (VO vo2 : vos) {
				i++;
				if (i == 1) {// 账号名称 R004
					String usrname = vo2.getAttr("usrname");
					message.append(String.format("%08d", usrname.getBytes("gb2312").length)
							+ "R004" + usrname);
				}
				String account = vo2.getAttr("account");
				message.append(String.format("%08d",
						account.getBytes("gb2312").length));
				message.append("T" + String.format("%03d", i) + account);
			}
		}
		// 计算报文长度并放在开头
		message.insert(0, String.format("%08d", message.toString().getBytes(
				"gb2312").length));
		// System.out.println("报文："+message);
		vo.setAttr("msg", message.toString());
		return vo;
	}

	/**
	 * 检索数据库进行账户验证
	 * 
	 * @return Vector 
	 * @throws Exception
	 */
	private Vector<VO> checkAccount(VO arg) throws Exception {
		StringBuffer sql = new StringBuffer(100);
		sql.append("SELECT * FROM accountnum");
		sql.append(" WHERE cardid='" + arg.getAttr("CARDID") + "'");
		// sql.append(" AND accounttype='"+arg.getAttr("ACCOUNTTYPE")+"'");
		CommonUtil.debug(sql.toString());
		VOList list = jw.readAll(new VO(), sql.toString());
		if (list != null && list.listData != null && 0 < list.listData.size())
			return list.listData;
		return null;
		// return true;
	}

	// /**
	// * 获取账号集合
	// * @param arg
	// * @return
	// * @throws Exception
	// */
	// private Vector<VO> getAccounts(VO arg) throws Exception{
	// String sql = "SELECT * FROM accountnum";
	// VOList list = jw.readAll(new VO(), sql);
	// return list.listData;
	// }
	//	
	// private long getBizid() throws Exception{
	// JdbcWorker jw = new JdbcWorker();
	// long INSTANCEID = jw.getSeqValue("cmdjb_bizid");
	//		return INSTANCEID;	
	//	}
}
